<?php
class TaptinsController extends AppController {

	var $name = 'Taptins';
	var $components = array('Uploader.Uploader');
    function beforeFilter(){
        $this->Uploader->uploadDir = 'files/uploads/';
        $this->Uploader->enableUpload = true;
        //$this->Uploader->maxFileSize = '2M'; 
        //$this->Uploader->maxNameLength = 25;
        $this->Uploader->tempDir = TMP;
    }
	
	function index() {
		$this->Taptin->recursive = 0;
		$this->set('taptins', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Tập tin ko tồn tại!', true));
			$this->redirect(array('action' => 'fileupload'));
		}
		
		$this->set('taptin', $this->Taptin->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Taptin->create();
			if ($this->Taptin->save($this->data)) {
				$this->Session->setFlash(__('The taptin has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The taptin could not be saved. Please, try again.', true));
			}
		}
		$khoas = $this->Taptin->Khoa->find('list');
		$this->set(compact('khoas'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid taptin', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Taptin->save($this->data)) {
				$this->Session->setFlash(__('The taptin has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The taptin could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Taptin->read(null, $id);
		}
		$khoas = $this->Taptin->Khoa->find('list');
		$this->set(compact('khoas'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Tập tin không tồn tại', true));
			$this->redirect(array('action'=>'index'));
		}
		$taptin = $this->Taptin->read(null, $id);
		if($this->Uploader->delete($taptin['Taptin']['link']))
		{
			if ($this->Taptin->delete($id)) {
				$this->Session->setFlash(__('Đã xóa tập tin', true),'success');
				$this->redirect(array('action'=>'fileupload'));
			}
		}
		$this->Session->setFlash(__('Không thể xóa tập tin', true),'error');
		$this->redirect(array('action' => 'fileupload'));
	}

	function upload() {
        if (!empty($this->data)) {
             if ($data = $this->Uploader->upload('fileName'))
             {
             	$arr = split('/',$data['path']);
             	//debug($data);
             	$newname= date('smhidmY').'_'.str_replace('','_',$arr[count($arr)-1]);
             	$arr[count($arr)-1]=$newname;
             	$this->Uploader->rename($data['path'],implode('/', $arr));
             	$data['path']=implode('/', $arr);
             	$record['tieude']=$this->data['Taptin']['tieude'];
             	$record['noidung']=$this->data['Taptin']['noidung'];
             	$record['link']=$data['path'];
             	$record['ngaykt']=$data['uploaded'];
             	$record['tenfile']=$newname;
             	$record['kieu']=$data['type'];
             	$record['dolon']=$data['filesize'];
             	$record['khoa_id']=$this->Session->read('khoa_id');
             	$record['z1user_id']=$this->Session->read('userid');
             	$this->Taptin->create();
             	$this->Taptin->save($record);
             	$this->Session->setFlash(__('Upload thành công!',true),'success');
             	$this->redirect(array('action'=>'fileupload'));
             }else
             {
             	$this->Session->setFlash('Lỗi khi upload.');
             	
             }
        }
    }

    function fileupload()
    {
    	$this->Taptin->recursive = 0;
		$this->set('taptins', $this->paginate(array('Taptin.khoa_id'=>$this->Session->read('khoa_id'))));
    }

    function download($id=null)
    {
   		if (!$id)
   		{
			$this->Session->setFlash(__('Tập tin ko tồn tại!', true));
			$this->redirect(array('action' => 'fileupload'));
		}
		$taptin = $this->Taptin->read(null, $id);
		 header("Content-Type: application/octet-stream");
        header("Content-Disposition: attachment; filename=".$taptin['Taptin']['tenfile']);
        
        header("Content-Transfer-Encoding: binary");
        header("Content-Length: " . filesize(WWW_ROOT.$taptin['Taptin']['link']));
        header("Content-Length: " . filesize(WWW_ROOT.$taptin['Taptin']['link']));
        readfile(WWW_ROOT.$taptin['Taptin']['link']);
        $this->Session->setFlash(__('Download thanh cong!!'));       
    }
    
}
